package com.cwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwz.entity.ApiAccessLog;
import com.cwz.entity.Material;
import com.cwz.entity.User;
import com.cwz.exception.ErrorEnum;
import com.cwz.exception.ServiceException;
import com.cwz.mapper.UserMapper;
import com.cwz.param.UserQueryParam;
import com.cwz.service.UserService;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wenZhen
 * @create 2024-08-17 11:14
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService {



    @Resource
    private UserMapper userMapper;
    @Override
    public Map<String, Object> login(User user, HttpSession session) {

//        QueryWrapper<User> wrapper=new QueryWrapper<User>();
//        wrapper.eq("ACCOUNT", user.getAccount());
//        userMapper.selectOne(wrapper);


        User one=userMapper.getByAccount(user.getAccount());
        log.info("user:{}",one);
        if (one==null){
            throw new ServiceException(ErrorEnum.ACCOUNT_NOT_EXISTS);
        }
        if (one.getStatus()==1){
            throw new ServiceException(ErrorEnum.ACCOUNT_IS_DISABLE);
        }
        if (!one.getPassword().equals(user.getPassword())){
            throw new ServiceException(ErrorEnum.PASSWORD_ERROR);
        }

        session.setAttribute("user",one);
        Map<String,Object> respMap=new HashMap<>();
        respMap.put("username",one.getUsername());
        respMap.put("role",one.getRole());
        respMap.put("id",one.getId());

        log.info("session:{}",session.getAttribute("user"));
        
        return success(respMap);
    }


    @Override
    public Map<String, Object> save(User user, HttpSession session) {

        checkAdminRole(session);

        String account = user.getAccount();

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ACCOUNT",account);
        User one = userMapper.selectOne(queryWrapper);

        if (one!=null){
            throw new ServiceException(ErrorEnum.ACCOUNT_IS_EXIST);
        }

        user.setRole(user.getRole());
        //自动生成密码
        user.setPassword("12345");

        userMapper.insert(user);
        return success();
    }

    @Override
    public Map<String, Object> page(UserQueryParam param, HttpSession session) {
        checkAdminRole(session);
        QueryWrapper queryWrapper=new QueryWrapper();
        IPage<User> page=new Page<>(param.getCurrent(),param.getSize());

        queryWrapper.like(!StringUtils.isNullOrEmpty(param.getUsername()),"USERNAME",param.getUsername())
                .eq(param.getStatus()!=null,"STATUS",param.getStatus())
                .eq(!StringUtils.isNullOrEmpty(param.getRole()),"ROLE",param.getRole());

        page= userMapper.selectPage(page, queryWrapper);

        return success(page);
    }

    @Override
    public Map<String, Object> update(User user, HttpSession session) {
        checkAdminRole(session);
        if (userMapper.selectById(user.getId()) == null) {
            throw new ServiceException(ErrorEnum.ACCOUNT_NOT_EXISTS);
        }
        userMapper.updateById(user);
        return success();
    }

    @Override
    public Map<String, Object> register(User one) {
        if (userMapper.getByAccount(one.getAccount())!=null) {
            throw new ServiceException(ErrorEnum.ACCOUNT_IS_EXIST);
        }
        User user = new User();
        user.setAccount(one.getAccount());
        user.setUsername(one.getUsername());
        user.setPassword(one.getPassword());
        user.setStatus(0);
        user.setRole("普通用户");
        userMapper.insert(user);
        return success("注册成功");
    }


    @Override
    public Map<String, Object> disable(User user, HttpSession session) {
        return  update(user,session);
    }
}
